import time
from loguru import logger
from .douyin_api import DouyinAPI


class UserMonitor:
    def __init__(self):
        # 账号配置
        self.accounts = {
            '不二臣': {
                'cookies': {
                    "ttwid": "1%7CIVGzIzcsLH0ZojT6-otcEXChMmr2WM0_9cXLRi-RXPQ%7C1736228934%7C3bbdb15b93ef70271b017d32df02dffa97edb27c23d4146a9a6133309be51a2a",
                    "UIFID_TEMP": "c4a29131752d59acb78af076c3dbdd52744118e38e80b4b96439ef1e20799db035d8b8a85598dbbec138bbd10e6169a93eee7dcd9547b280600d451bcb04e8820ea96d909eaab8866901373ecc638ab6",
                    "douyin.com": "",
                    "s_v_web_id": "verify_m5m1vohu_1himLFgT_G56r_4IPA_8OH9_5nu8nHhl1M6K",
                    "device_web_cpu_core": "12",
                    "device_web_memory_size": "8",
                    "architecture": "amd64",
                    "hevc_supported": "true",
                    "home_can_add_dy_2_desktop": "%220%22",
                    "dy_swidth": "1920",
                    "dy_sheight": "1080",
                    "strategyABtestKey": "%221736228935.529%22",
                    "csrf_session_id": "e535ecd084a92d320f1d137ad33e6ed8",
                    "fpk1": "U2FsdGVkX1+iAiwYLMzaDamvITp3kRh/pckp+yBN2dxVHqmr1dGRYXYInYTA34FLxKMPXTT9keHWT8Umewnncw==",
                    "fpk2": "f51bb482c660d0eeadd1f058058a2b35",
                    "volume_info": "%7B%22isUserMute%22%3Afalse%2C%22isMute%22%3Afalse%2C%22volume%22%3A0.5%7D",
                    "passport_csrf_token": "2961ed752330d0a1e653c8950d56d09c",
                    "passport_csrf_token_default": "2961ed752330d0a1e653c8950d56d09c",
                    "FORCE_LOGIN": "%7B%22videoConsumedRemainSeconds%22%3A180%7D",
                    "UIFID": "c4a29131752d59acb78af076c3dbdd52744118e38e80b4b96439ef1e20799db035d8b8a85598dbbec138bbd10e6169a9f9cbf53116c7dacc6d8041085c2ab7e50c4b570af6aae31709a59ec0219111502c3d182a70afa76f3940e3014d6186ba54a857822384db7828d9417be0d2d998c217d9be12d7cf3cc2dfdc451480163051e43a14ebbfe2beb05afb879daf3e7026cc01c3d6be6bb5a871c1166af0f762",
                    "bd_ticket_guard_client_web_domain": "2",
                    "__ac_signature": "_02B4Z6wo00f01n2K-1wAAIDCSq4.AA7i6PJ9qv.AAPgte3",
                    "is_dash_user": "1",
                    "biz_trace_id": "0e41947f",
                    "passport_assist_user": "CjypTJe81evW2bqXehP5SOoJUfGAgf-AG1VPyHfvWvgNq6_bUeDeShFf3qB3hSAIiWfAhkWsFpw-0PmKHsEaSgo8XNKCHdy3eKHgr_vzwTJSTTWoT3ierzvdxRfvUeDSjCyrGBxLraIQLkD8zKwAJ4vMdm6Bco_2ouj_Y0kpEMuW5g0Yia_WVCABIgED6JkzCA%3D%3D",
                    "n_mh": "2ffm9Wl-31kM3WjNlYxkcXSyExLEajmhJ2RsC0Er8ks",
                    "sso_uid_tt": "f00bcb06f47dd0bc412980a08f775d4b",
                    "sso_uid_tt_ss": "f00bcb06f47dd0bc412980a08f775d4b",
                    "toutiao_sso_user": "3188a49c619f26ed4988b7e01af86f2c",
                    "toutiao_sso_user_ss": "3188a49c619f26ed4988b7e01af86f2c",
                    "sid_ucp_sso_v1": "1.0.0-KDg1NDIwOTAyNTA3Yzg5Y2QxNjMzZTIyNTk2ZDJmNzA1YWUwZDI5M2IKHwjT67DFpAIQ6ZfzuwYY7zEgDDDN_IrRBTgGQPQHSAYaAmhsIiAzMTg4YTQ5YzYxOWYyNmVkNDk4OGI3ZTAxYWY4NmYyYw",
                    "ssid_ucp_sso_v1": "1.0.0-KDg1NDIwOTAyNTA3Yzg5Y2QxNjMzZTIyNTk2ZDJmNzA1YWUwZDI5M2IKHwjT67DFpAIQ6ZfzuwYY7zEgDDDN_IrRBTgGQPQHSAYaAmhsIiAzMTg4YTQ5YzYxOWYyNmVkNDk4OGI3ZTAxYWY4NmYyYw",
                    "login_time": "1736231913920",
                    "passport_auth_status": "9c4dcc18a31213ff5230b249d56eeaae%2C",
                    "passport_auth_status_ss": "9c4dcc18a31213ff5230b249d56eeaae%2C",
                    "uid_tt": "b53ebdf15c5aa91e65ef401393f9e87c",
                    "uid_tt_ss": "b53ebdf15c5aa91e65ef401393f9e87c",
                    "sid_tt": "8cef74f7983e9288b4f53f3911da51dc",
                    "sessionid": "8cef74f7983e9288b4f53f3911da51dc",
                    "sessionid_ss": "8cef74f7983e9288b4f53f3911da51dc",
                    "is_staff_user": "false",
                    "publish_badge_show_info": "%220%2C0%2C0%2C1736231916621%22",
                    "SelfTabRedDotControl": "%5B%5D",
                    "_bd_ticket_crypt_doamin": "2",
                    "_bd_ticket_crypt_cookie": "33ced917abc825d66f1fa4424d5453d3",
                    "__security_server_data_status": "1",
                    "sid_guard": "8cef74f7983e9288b4f53f3911da51dc%7C1736231924%7C5183992%7CSat%2C+08-Mar-2025+06%3A38%3A36+GMT",
                    "sid_ucp_v1": "1.0.0-KDg5MGI1YTUxYjRlZTYyNWY2NzJkMTZjNjgwNTVhZjNiZDMxYzczYTEKGQjT67DFpAIQ9JfzuwYY7zEgDDgGQPQHSAQaAmxmIiA4Y2VmNzRmNzk4M2U5Mjg4YjRmNTNmMzkxMWRhNTFkYw",
                    "ssid_ucp_v1": "1.0.0-KDg5MGI1YTUxYjRlZTYyNWY2NzJkMTZjNjgwNTVhZjNiZDMxYzczYTEKGQjT67DFpAIQ9JfzuwYY7zEgDDgGQPQHSAQaAmxmIiA4Y2VmNzRmNzk4M2U5Mjg4YjRmNTNmMzkxMWRhNTFkYw",
                    "download_guide": "%223%2F20250107%2F0%22",
                    "my_rd": "2",
                    "FOLLOW_LIVE_POINT_INFO": "%22MS4wLjABAAAA3pufiIk9gUKzG9c7o3-PdJbL5WlPUJ37XK0pqNRTW2M%2F1736265600000%2F0%2F0%2F1736233421580%22",
                    "FOLLOW_NUMBER_YELLOW_POINT_INFO": "%22MS4wLjABAAAA3pufiIk9gUKzG9c7o3-PdJbL5WlPUJ37XK0pqNRTW2M%2F1736265600000%2F0%2F1736232821581%2F0%22",
                    "__live_version__": "%221.1.2.6953%22",
                    "webcast_local_quality": "null",
                    "live_can_add_dy_2_desktop": "%220%22",
                    "live_use_vvc": "%22false%22",
                    "stream_recommend_feed_params": "%22%7B%5C%22cookie_enabled%5C%22%3Atrue%2C%5C%22screen_width%5C%22%3A1920%2C%5C%22screen_height%5C%22%3A1080%2C%5C%22browser_online%5C%22%3Atrue%2C%5C%22cpu_core_num%5C%22%3A12%2C%5C%22device_memory%5C%22%3A8%2C%5C%22downlink%5C%22%3A10%2C%5C%22effective_type%5C%22%3A%5C%224g%5C%22%2C%5C%22round_trip_time%5C%22%3A100%7D%22",
                    "xg_device_score": "7.802204888412783",
                    "bd_ticket_guard_client_data": "eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtcmVlLXB1YmxpYy1rZXkiOiJCSVAzVUU2KzJBeWZiNC9zNmpFWGtkOGRhTnVOK25iSk1zWnNwSGpNeEJpM05va09tS3MvWDZSWFFJbm5La3dpaEhYSXlCalAwd2VvQkRFTGRGQkYzdTQ9IiwiYmQtdGlja2V0LWd1YXJkLXdlYi12ZXJzaW9uIjoyfQ%3D%3D",
                    "xgplayer_device_id": "44999160127",
                    "xgplayer_user_id": "573981007853",
                    "odin_tt": "5950553ab836f8fb43278d5f1f0461948202bcb23184507e1b27c46c53c3f2fed67a94a1ab275760d595a0ab8ff397f2",
                    "passport_fe_beating_status": "false",
                    "IsDouyinActive": "true"
                }

            },
            'account2': {
                'cookies': {
                    "ttwid": "1%7CGiH7attQL2qPnUM-1YMbQnLWiixa9dmYmZDjPNWtP30%7C1735279336%7C2b4c8c39a33fd849265c6757b2954cad1f751f41af865f84fb50b65657960435",
                    "x-web-secsdk-uid": "656f9394-941c-48cc-bc20-d146ff6c0af5",
                    "__live_version__": "%221.1.2.6631%22",
                    "has_avx2": "null",
                    "device_web_cpu_core": "12",
                    "device_web_memory_size": "8",
                    "webcast_local_quality": "null",
                    "live_can_add_dy_2_desktop": "%220%22",
                    "live_use_vvc": "%22false%22",
                    "csrf_session_id": "6f684b2ed467c9c6dcc38a507cc050ad",
                    "volume_info": "%7B%22isMute%22%3Afalse%2C%22isUserMute%22%3Afalse%7D",
                    "fpk1": "U2FsdGVkX1+/NnJELo0lyfcYY0tEix+lONXi1SGht0G3BjihXMkEfJxQ4gavR39yIgu5ddGBgig+IQOjk5n4zA==",
                    "fpk2": "f51bb482c660d0eeadd1f058058a2b35",
                    "FORCE_LOGIN": "%7B%22videoConsumedRemainSeconds%22%3A180%2C%22isForcePopClose%22%3A1%7D",
                    "__security_mc_1_s_sdk_crypt_sdk": "d37a42cd-4970-88ac",
                    "bd_ticket_guard_client_web_domain": "2",
                    "passport_csrf_token": "3a2c490c41c6fa3e93cb2304bb28c918",
                    "passport_csrf_token_default": "3a2c490c41c6fa3e93cb2304bb28c918",
                    "gulu_source_res": "eyJwX2luIjoiNGEyZmE1ZTg5YTg1M2ViNDJiOTRmMzNjODI3MThlYzAyMDdmNDc5ZjdhYTgxNmE5ZjlmZmNjNmI3OGFhZWZmNiJ9",
                    "s_v_web_id": "verify_m56cssew_5OKHRTtW_5Tzs_4YX8_Ai3G_6OhdqH7A39mo",
                    "sdk_source_info": "7e276470716a68645a606960273f276364697660272927676c715a6d6069756077273f276364697660272927666d776a68605a607d71606b766c6a6b5a7666776c7571273f275e58272927666a6b766a69605a696c6061273f27636469766027292762696a6764695a7364776c6467696076273f275e5827292771273f2734323234373d3c3237303632342778",
                    "bit_env": "IIW1x4jjM0s9rHPthNkoCJIfiBqakeDZSeQyb5AaJUJEIuHeWFigk1Q0-Aa_6U8h1vqSQwmSslT6kPXQOLSlqI5XKek7sYuy--nPnPe2y0GnuOM7rc41amwvpICxrwcq4ce4UCUzYF-YQuh6b3Y_waG3XMYMQHeX6PPY_hzNL6K3_yKyhQAKmequMSXMV5YNxfqi9s_nx-5TM2EteBRu1D1jyxnBMI5XmlNAYu01x2EqJQeW_9CeqSeZWf6-YPb2_TlunGEphgZnmZRLT90jZdlvhoKlIHSzMo_HIRN-jvz3YLoxFv5Fg9NG1mIGCycU6hiiEEUOPAUBkwyGbCyPxLZ1JgPrN1hjJS0edDckbBr358QzTckC4wcxB34g7Sc8eYwr5OgPpMAC4-BOlY9lDN9vKofUR7-Yw4xfrHJcyJtXCEWexVVjpE7roDqsD4v0WWU8oMj0Y_9qRmDL1022Ki5yQKpocXhUivvgLoCrSqRQRsTUl9TRp0qTqk6cgXdo",
                    "passport_auth_mix_state": "pzf3ik7725hingcrdte05v8j5dubm9ea",
                    "biz_trace_id": "b2d7145b",
                    "download_guide": "%222%2F20241227%2F0%22",
                    "passport_mfa_token": "CjcxpNF8qoENlqr8t%2B8E7hrrURstJau2nDL%2FugWl%2BRwMrHJy7NhDgJYoqaPxErigQ%2FOY1CT44q%2BgGkoKPIu0QhxRKZYaRqej8AkXqvguL%2BQJUxwgsUK%2B%2B0obzElc2AeifcuAopjeyr5Ve1osXeiyUUYMbiqUV%2Fe7WBCImuUNGPax0WwgAiIBAzCyvao%3D",
                    "d_ticket": "7cc1f1efff7e401a088e2ddb2b22448ff08d5",
                    "passport_assist_user": "CkGq_qx1rzbH7xVy_7fQtABkUr5s0dAT_fBLtiKLvjvcxOfpBR6EDxxzZ60mTcwCejkz5yV5vsnLWEjtrTmQmYi6zRpKCjxT_mhl2mGwC09XpngzkTV7ESIBAC4-Oc1WKONVUJYSDyfKDmsizDqJcoE3kn5VdvA5SbipGAu0XgEn9WYQi5vlDRiJr9ZUIAEiAQP7HnZj",
                    "n_mh": "aGFTpOZ-1sMoBdbEaKGITb-EZkY3xwuzNGQ1T5mFPn0",
                    "sso_uid_tt": "c523b97e389f8891f07c039901c6a448",
                    "sso_uid_tt_ss": "c523b97e389f8891f07c039901c6a448",
                    "toutiao_sso_user": "b82c7f9522c803ee09fb9b794928ff20",
                    "toutiao_sso_user_ss": "b82c7f9522c803ee09fb9b794928ff20",
                    "sid_ucp_sso_v1": "1.0.0-KDg4YTNmM2FlNTk4N2IxZGRiOWE1MDlhNGNhMDdlMmY5ZDAwNWUwZWYKIQjst_C7yMylBBDVirm7BhjvMSAMMM3vobIGOAZA9AdIBhoCbGYiIGI4MmM3Zjk1MjJjODAzZWUwOWZiOWI3OTQ5MjhmZjIw",
                    "ssid_ucp_sso_v1": "1.0.0-KDg4YTNmM2FlNTk4N2IxZGRiOWE1MDlhNGNhMDdlMmY5ZDAwNWUwZWYKIQjst_C7yMylBBDVirm7BhjvMSAMMM3vobIGOAZA9AdIBhoCbGYiIGI4MmM3Zjk1MjJjODAzZWUwOWZiOWI3OTQ5MjhmZjIw",
                    "__security_mc_1_s_sdk_sign_data_key_sso": "345b7950-49fc-9552",
                    "__security_mc_1_s_sdk_sign_data_key_sso_time": "4130830d-41c2-9110",
                    "__security_mc_1_s_sdk_cert_key": "ef882d8f-4b56-9c87",
                    "login_time": "1735279957650",
                    "passport_auth_status": "02ab280272dc68d5327d6ea7e5707637%2C",
                    "passport_auth_status_ss": "02ab280272dc68d5327d6ea7e5707637%2C",
                    "uid_tt": "cd4926736082bc88004008e6249e9ac5",
                    "uid_tt_ss": "cd4926736082bc88004008e6249e9ac5",
                    "sid_tt": "bcbf17df85f372b76f7e54c5a21e4452",
                    "sessionid": "bcbf17df85f372b76f7e54c5a21e4452",
                    "sessionid_ss": "bcbf17df85f372b76f7e54c5a21e4452",
                    "is_staff_user": "false",
                    "publish_badge_show_info": "%220%2C0%2C0%2C1735279961621%22",
                    "_bd_ticket_crypt_doamin": "2",
                    "_bd_ticket_crypt_cookie": "ad583edf068e818e135ffe9f82d79d22",
                    "__security_mc_1_s_sdk_sign_data_key_web_protect": "bb65cb04-4235-b112",
                    "__security_server_data_status": "1",
                    "__security_mc_1_s_sdk_sign_data_key_web_protect_time": "06c2501a-458e-a745",
                    "sid_guard": "bcbf17df85f372b76f7e54c5a21e4452%7C1735279962%7C5183998%7CTue%2C+25-Feb-2025+06%3A12%3A40+GMT",
                    "sid_ucp_v1": "1.0.0-KDg2NGY3ZWNjNjI4ZGJmYmFjNGYzNjQ3ZWI1YmY4MjM4NGIxYjQ0MzIKGwjst_C7yMylBBDairm7BhjvMSAMOAZA9AdIBBoCaGwiIGJjYmYxN2RmODVmMzcyYjc2ZjdlNTRjNWEyMWU0NDUy",
                    "ssid_ucp_v1": "1.0.0-KDg2NGY3ZWNjNjI4ZGJmYmFjNGYzNjQ3ZWI1YmY4MjM4NGIxYjQ0MzIKGwjst_C7yMylBBDairm7BhjvMSAMOAZA9AdIBBoCaGwiIGJjYmYxN2RmODVmMzcyYjc2ZjdlNTRjNWEyMWU0NDUy",
                    "SelfTabRedDotControl": "%5B%5D",
                    "UIFID_TEMP": "c4a29131752d59acb78af076c3dbdd52744118e38e80b4b96439ef1e20799db0b4212209eaafe6e5d81baccecbf397517224d6281e73dfa95822866d79abfe64f24739144072c6966580e547d4923ef9",
                    "hevc_supported": "true",
                    "home_can_add_dy_2_desktop": "%220%22",
                    "stream_recommend_feed_params": "%22%7B%5C%22cookie_enabled%5C%22%3Atrue%2C%5C%22screen_width%5C%22%3A1920%2C%5C%22screen_height%5C%22%3A1080%2C%5C%22browser_online%5C%22%3Atrue%2C%5C%22cpu_core_num%5C%22%3A12%2C%5C%22device_memory%5C%22%3A8%2C%5C%22downlink%5C%22%3A10%2C%5C%22effective_type%5C%22%3A%5C%224g%5C%22%2C%5C%22round_trip_time%5C%22%3A150%7D%22",
                    "my_rd": "2",
                    "FOLLOW_LIVE_POINT_INFO": "%22MS4wLjABAAAA3t9jnkmMyajMpy5YPH8OCHG0Iz55eoeo4RQzDlQv4Lhzqem7PHf1Js85EaQ0-_FB%2F1735315200000%2F1735279975337%2F1735279963107%2F0%22",
                    "is_dash_user": "1",
                    "bd_ticket_guard_client_data": "eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtcmVlLXB1YmxpYy1rZXkiOiJCQlJrNWYzVlVhUlk4ZTNqanRpMXgwVndSclFlM05mM1Q3S2JhNW12bmpTaEh5cjNmVWRVblkyYmRQdFZ6OVI0K0dVcnVIWXd0cHVKUVNubmRvalBRRms9IiwiYmQtdGlja2V0LWd1YXJkLXdlYi12ZXJzaW9uIjoyfQ%3D%3D",
                    "passport_fe_beating_status": "true",
                    "IsDouyinActive": "true",
                    "odin_tt": "6c920f090bf08cb7f903f390a8443a451866ff3fc8fd35a27e55e8988d8adbc45e538a9c98ff727afb0946d3eb84450ddc64a77d8c7524cd633b6ea5698f0bcb"
                }
            }
        }

        # 初始化API客户端
        self.api_clients = {
            account_id: DouyinAPI(account_id, account['cookies'])
            for account_id, account in self.accounts.items()
        }

    def monitor_user(self):
        """监控多个账号的关注直播"""
        while True:
            for account_id, api_client in self.api_clients.items():
                try:
                    # 获取直播间列表
                    room_list = api_client.get_following_rooms()

                    # 添加到监控
                    if room_list:
                        api_client.add_rooms_to_monitor(room_list)

                except Exception as e:
                    logger.error(f"监控账号 {account_id} 出错: {e}")

            time.sleep(180)  # 3分钟检查一次
